package leetcode.sort;

import java.util.Arrays;

public class InsertSort {
    public int[] insert_sort(int[] sourceArray) {
        int[] arr = Arrays.copyOf(sourceArray, sourceArray.length);

        for (int i = 1; i < arr.length; i++) {
            int tmp = arr[i];

            int j = i;

            while (j > 0 && tmp < arr[j - 1]) {
                arr[j] = arr[j - 1];
                j--;
            }

            if (j != i) {
                arr[j] = tmp;
            }
        }

        return arr;
    }

    public static void main(String[] args) {
        InsertSort insertSort = new InsertSort();
        int[] arr = new int[]{1, 3, 5, 7, 3, 9, 6, 3};
        int[] ret = insertSort.insert_sort(arr);
        for (int i : ret) {
            System.out.print(i + " ");
        }
    }
}
